package ciyuan.ChangeMinecraft;

import ciyuan.Item.Food.Shit;
import ciyuan.Item.ItemRegisterManager;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerContainerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import static ciyuan.Item.ItemGroups.Life;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber
public class FoodRot {

    private static final String FoodTimeString = "ciyuanrpgfoodtime";

    @SubscribeEvent
    public static void onInventory(PlayerContainerEvent event) {
        for(Slot slot: event.getContainer().slots) {
            ItemStack itemStack = slot.getItem();
            if (itemStack.getItem() instanceof Shit) {
                continue;
            }

            if (itemStack.getItem().getFoodProperties() != null && !itemStack.getItem().getCreativeTabs().contains(Life)) {
                CompoundNBT compoundNBT = itemStack.getTag();
                if (compoundNBT == null) {
                    compoundNBT = new CompoundNBT();
                }
                if (compoundNBT.contains(FoodTimeString)) {
                    long foodTime = compoundNBT.getLong(FoodTimeString);
                    if (event.getPlayer().getCommandSenderWorld().getGameTime() - foodTime > 100000) {
                        slot.set(new ItemStack(ItemRegisterManager.Shit.get()));
                    }
                } else {
                    long foodTime = event.getPlayer().getCommandSenderWorld().getGameTime();
                    compoundNBT.putLong(FoodTimeString,foodTime);
                }
                itemStack.setTag(compoundNBT);
            }
        }

    }

    @SubscribeEvent
    public static void onPickUpItem(EntityItemPickupEvent event) {

        if (event.getItem().getItem().getItem() instanceof Shit) {
            return;
        }
        ItemStack itemStack = event.getItem().getItem();
        if (itemStack.getItem().getFoodProperties() != null && !itemStack.getItem().getCreativeTabs().contains(Life)) {
            CompoundNBT compoundNBT = itemStack.getTag();
            if (compoundNBT == null) {
                compoundNBT = new CompoundNBT();
            }
            if (compoundNBT.contains(FoodTimeString)) {
                long foodTime = compoundNBT.getLong(FoodTimeString);
                if (event.getPlayer().getCommandSenderWorld().getGameTime() - foodTime > 100000) {
                    event.getItem().setItem(new ItemStack(ItemRegisterManager.Shit.get()));
                }
            } else {
                long foodTime = event.getPlayer().getCommandSenderWorld().getGameTime();
                compoundNBT.putLong(FoodTimeString,foodTime);
            }

            for (ItemStack item : event.getPlayer().inventory.items) {
                CompoundNBT compoundNBT1 = item.getTag();
                if (compoundNBT1==null) {
                    continue;
                }
                if (compoundNBT1.contains(FoodTimeString) && itemStack.getItem().getDescriptionId().equals(item.getItem().getDescriptionId())) {
                    long foodTime2 = compoundNBT1.getLong(FoodTimeString);
                    long foodTime1 = compoundNBT.getLong(FoodTimeString);
                    if (Math.abs(foodTime1-foodTime2)<2000){
                        compoundNBT.putLong(FoodTimeString,(foodTime1+foodTime2)/2);
                        compoundNBT1.putLong(FoodTimeString,(foodTime1+foodTime2)/2);
                        item.setTag(compoundNBT1);
                    }
                }
            }
            itemStack.setTag(compoundNBT);
        }
    }

}
